#include "qtabledialog.h"
#include "ui_qtabledialog.h"
#include <qDebug>
#include <QResizeEvent>
#include <QVBoxLayout>
#include "window.h"

QTableDialog::QTableDialog(QWidget *parent,bool isTotal) :
    QDialog(parent),
    ui(new Ui::QTableDialog),
    total(isTotal)
{
    ui->setupUi(this);

    setTableHeader();

    if(isTotal){
        ui->btnToCurve->hide();
    }
}

QTableDialog::~QTableDialog()
{
    delete ui;
}

void QTableDialog::createItemsARow(int rowNo, std::vector<std::pair<int,float>> &range,QString &htxt){

    for(auto t:range)
        qDebug()<<t.second;
    rowNo = ui->table->rowCount();
    ui->table->insertRow(rowNo);

    QTableWidgetItem *rowItem;
    for(int i = 0;i<11;i++){
        if(i==0){
            rowItem = new QTableWidgetItem(htxt);
            auto font = rowItem->font();
            font.setBold(true);
            font.setPointSize(12);
            rowItem->setFont(font);
            rowItem->setTextAlignment(Qt::AlignCenter);
            ui->table->setItem(rowNo,0,rowItem);
        }else{
            rowItem = new QTableWidgetItem(QString().setNum(range[i-1].second,'f',3));
            rowItem->setTextAlignment(Qt::AlignCenter);
            ui->table->setItem(rowNo,i,rowItem);
        }

    }
    ui->table->resizeColumnsToContents();
    ui->table->resizeRowsToContents();
}

void QTableDialog::insertItemsARow(QTableDialog *tbl,QString &htxt){
    QTableWidget *curTable = (QTableWidget*)tbl->getCurTbl();
    std::vector<std::pair<int,float>>range;
    int cC = curTable->columnCount();
    range.resize(cC);
    for(int i=1;i<cC;i++){
        range[i-1].second = curTable->item(0,i)->text().toFloat();
    }
    for(auto t:range){
        qDebug()<<t.second;
    }
    createItemsARow(0,range,htxt);
}

void QTableDialog::on_btnToCurve_clicked()
{
    if(chart==nullptr){
        chart = new Chart(nullptr,this->windowTitle());
        int rows = ui->table->rowCount();
        int cols = ui->table->columnCount();

        while(rows){
            QVector<qreal>value(cols-1);
            for(int i=1;i<11;i++){
                value[i-1]=ui->table->item(rows-1,i)->text().toDouble();
            }

            chart->addSeries(((Window*)this->parent())->getModelName(),value);

            --rows;
        }

    }
    chart->show();
}

//private functions
void QTableDialog::setTableHeader(){
    QTableWidgetItem *headerItem;
    QStringList headerList;
    headerList<<"range"<<"0.1"<<"0.2"<<"0.3"<<"0.4"<<"0.5"
             <<"0.6"<<"0.7"<<"0.8"<<"0.9"<<"1.0";
    ui->table->setColumnCount(headerList.count());

    for(int i=0;i<headerList.count();++i){
        headerItem = new QTableWidgetItem(headerList.at(i));
        auto font = headerItem->font();
        if(i==0){
            font.setBold(true);

            font.setPointSize(12);
            headerItem->setFont(font);
        }
        headerItem->setTextAlignment(Qt::AlignCenter);
        ui->table->setHorizontalHeaderItem(i,headerItem);
    }

    ui->table->verticalHeader()->setVisible(false);
}

void *QTableDialog::getCurTbl(){
    return ui->table;
}
